/************************************************************************************************************************************************************************************************************************************************
Can Mentoring Alleviate Family Disadvantage in Adolescence? A Field Experi-ment to Improve Labor-Market Prospects
by Sven Resnjanskij, Jens Ruhose, Simon Wiederhold, Ludger Woessmann, and Katharina Wedel

Journal of Political Economy

Replication code for generating the PISA data for comparison (Appendix Table A1)
************************************************************************************************************************************************************************************************************************************************/

//  STATA Set-up 
cap log close
clear all
timer clear
version 15.1
set rmsg off, perm

// Select Path & working directory
global path "mydirectory" 
cd "${path}"

// Folder and paths
foreach folder in data prog {
  global `folder' ${path}\\`folder'
}


// load data
use "${data}\\pisa_2012.dta"

*********************************************************************************
** SAMPLE SELECTION
*********************************************************************************

keep if CNT == "DEU"
rename ST28Q01 books
replace books =. if books > 6
keep if books != . // (Do not have missing books in the RYL dataset)


*********************************************************************************
** NEW VARIABLES AND LABELS
*********************************************************************************

* Books
tab books, gen(dbooks)
recode books (1 2 = 0) (3 4 5 6 = 1), gen(books_coarse)

* University status
gen muni = misced
replace muni = 0 if muni<6
replace muni = 1 if muni==6
replace muni =. if muni==9
tab muni misced, m

gen funi = fisced
replace funi = 0 if funi<6
replace funi = 1 if funi==6
replace funi =. if funi==9
tab funi fisced, m

gen huni = hisced
replace huni = 0 if huni<6
replace huni = 1 if huni==6
replace huni =. if huni==9
tab huni hisced, m

rename muni uni_m
rename funi uni_f
la def university 0 "No uni educ" 1 "Uni educ" 
la val uni_m uni_f university

tab uni_m, gen(duni_m)
	gen uni_mMD = (uni_m==.)
tab uni_f, gen(duni_f)
	gen uni_fMD = (uni_f==.)

* Highest education status of parents
gen uni_par = (uni_m == 1 | uni_f == 1)
	replace uni_par = . if uni_m == . & uni_f == .
	gen uni_parMD = (uni_par==.)

recode uni_par (.=0), gen(uni_parSES)
	
	
* Employment status
recode ST15Q01 (7 8 9=.) (3=0) (4=1) (2=2) (1=3), gen(empl_m)
recode ST19Q01 (7 8 9=.) (3=0) (4=1) (2=2) (1=3), gen(empl_f)
la def employment  0 "Unemployed" 1 "Not employed, others" 2 "Part-time employed" 3 "Full-time employed", replace
la val empl_m empl_f employment
tab empl_m
tab empl_f

tab empl_m, gen(dempl_m)
	gen empl_mMD = (empl_m==.)
tab empl_f, gen(dempl_f)
	gen empl_fMD = (empl_f==.) 

gen empl_mfulltime = (dempl_m4==1)
	replace empl_mfulltime = . if empl_mMD == 1
gen empl_ffulltime = (dempl_f4==1)
	replace empl_ffulltime = . if empl_fMD == 1
	
* Employment of the parents
gen empl_par = (empl_mfulltime == 1 | empl_ffulltime == 1)
	
* 1st generation migrant
recode ST20Q01 (8 9 = .) (2 = 1) (1 = 0), gen(migrant_first)
la def migrant_first 1 "1st Migrant" 0 "Non-migrant"
la val migrant_first migrant_first
tab migrant_first, gen(dmigrant_first)
gen nonmigrant_first = 1- migrant_first

recode nonmigrant_first (.=0), gen(nonmigrant_firstSES)

* 1st and 2nd generation migrant
gen migrant = migrant_first
replace migrant = 1 if ST20Q02!=1 | ST20Q03!=1
tab migrant

//Single parent
gen singleparent=0
replace singleparent = 1 if (ST11Q01==2 | ST11Q02==2)
tab singleparent ,m
bysort books_coarse: tab singleparent ,m
gen nonsingleparent = 1-singleparent

recode nonsingleparent (.=0), gen(nonsingleparentSES)

recode ST04Q01 (2=1) (1=0), gen(male)
rename AGE age

*********************************************************************************
** SES Measure RYL
*********************************************************************************

#delimit ;
gen HigherSES =  nonmigrant_firstSES==1 & 
				(books_coarse == 1 | uni_parSES == 1) &
				(books_coarse == 1 | nonsingleparentSES == 1)
;
#delimit cr


#delimit ;
gen LowSES = nonmigrant_firstSES==0  |
			 books_coarse == 0 & uni_parSES == 0 | 
			 books_coarse == 0 & nonsingleparentSES == 0
;
#delimit cr


************************************************************************************************************************************************************************************************************************************************
** AVERAGES FOR COMPARISON WITH RYL SAMPLE
************************************************************************************************************************************************************************************************************************************************

drop duni*
replace uni_f = 9 if missing(uni_f) 
tab uni_f, gen(duni_f) 

replace uni_m = 9 if missing(uni_m) 
tab uni_m , gen(duni_m)

replace uni_par = 9 if missing(uni_par)
tab uni_par, gen(duni_par)

drop dempl*
replace empl_f = 9 if empl_f ==9 |  missing(empl_f)
tab empl_f, gen(dempl_f)
sum empl_f

replace empl_m = 9 if empl_m ==9 |  missing(empl_m)
tab empl_m, gen(dempl_m)
sum empl_m

expand 2, gen(samples)

replace samples = 2 if samples == 1 & HigherSES == 1

#delimit;
local varlist LowSES dbooks1 dbooks2 dbooks3 dbooks4 dbooks5 dbooks6 migrant migrant_first age male singleparent  
duni_f1 duni_f2 duni_f3 duni_m1 duni_m2 duni_m3 duni_par1 duni_par2 duni_par3 dempl_f1 dempl_f2 dempl_f3 dempl_f4 dempl_f5 dempl_m1 dempl_m2 dempl_m3 dempl_m4 dempl_m5
;
#delimit cr

foreach xx in `varlist'{
    rename `xx' mean`xx'
}

foreach xx in `varlist'{
    gen sd`xx' = mean`xx'
	gen obs`xx' = mean`xx'
}


#delimit;
local meanvarlist meanLowSES meandbooks1 meandbooks2 meandbooks3 meandbooks4 meandbooks5 meandbooks6 meanmigrant meanmigrant_first meanage meanmale meansingleparent meanduni_f1 meanduni_f2 meanduni_f3 meanduni_m1 meanduni_m2 meanduni_m3 meanduni_par1 meanduni_par2 meanduni_par3 meandempl_f1 meandempl_f2 meandempl_f3 meandempl_f4 meandempl_f5 meandempl_m1 meandempl_m2 meandempl_m3 meandempl_m4 meandempl_m5;
local sdvarlist sdLowSES sddbooks1 sddbooks2 sddbooks3 sddbooks4 sddbooks5 sddbooks6 sdmigrant sdmigrant_first sdage sdmale sdsingleparent  
sdduni_f1 sdduni_f2 sdduni_f3 sdduni_m1 sdduni_m2 sdduni_m3 sdduni_par1 sdduni_par2 sdduni_par3 sddempl_f1 sddempl_f2 sddempl_f3 sddempl_f4 sddempl_f5 sddempl_m1 sddempl_m2 sddempl_m3 sddempl_m4 sddempl_m5;
local obsvarlist obsLowSES obsdbooks1 obsdbooks2 obsdbooks3 obsdbooks4 obsdbooks5 obsdbooks6 obsmigrant obsmigrant_first obsage obsmale obssingleparent obsduni_f1 obsduni_f2 obsduni_f3 obsduni_m1 obsduni_m2 obsduni_m3 obsduni_par1 obsduni_par2 obsduni_par3 obsdempl_f1 obsdempl_f2 obsdempl_f3 obsdempl_f4 obsdempl_f5 obsdempl_m1 obsdempl_m2 obsdempl_m3 obsdempl_m4 obsdempl_m5;
#delimit cr

#delimit;
collapse (mean) `meanvarlist' (sd) `sdvarlist' (count) `obsvarlist', by(samples)
;
#delimit cr


reshape long mean sd obs, i(samples) j(variable, string)
rename mean meanPISA
rename sd sdPISA
rename obs obsPISA

reshape wide mean sd obs, i(variable) j(samples)

replace variable = "Low-SES" if variable == "LowSES"
replace variable = "books_010" if variable == "dbooks1"
replace variable = "books_1125" if variable == "dbooks2"
replace variable = "books_26100" if variable == "dbooks3"
replace variable = "books_101200" if variable == "dbooks4"
replace variable = "books_201500" if variable == "dbooks5"
replace variable = "books_m500" if variable == "dbooks6"
replace variable = "empl_f_unemployed" if variable == "dempl_f1"
replace variable = "empl_f_notemployedothers" if variable == "dempl_f2"
replace variable = "empl_f_parttime" if variable == "dempl_f3"
replace variable = "empl_f_fulltime" if variable == "dempl_f4"
replace variable = "empl_f_missing" if variable == "dempl_f5"
replace variable = "empl_m_unemployed" if variable == "dempl_m1"
replace variable = "empl_m_notemployedothers" if variable == "dempl_m2"
replace variable = "empl_m_parttime" if variable == "dempl_m3"
replace variable = "empl_m_fulltime" if variable == "dempl_m4"
replace variable = "empl_m_missing" if variable == "dempl_m5"
replace variable = "uni_f_no" if variable == "duni_f1"
replace variable = "uni_f_yes" if variable == "duni_f2"
replace variable = "uni_f_missing" if variable == "duni_f3"
replace variable = "uni_m_no" if variable == "duni_m1"
replace variable = "uni_m_yes" if variable == "duni_m2"
replace variable = "uni_m_missing" if variable == "duni_m3"
replace variable = "uni_parents_no" if variable == "duni_par1"
replace variable = "uni_parents_yes" if variable == "duni_par2"
replace variable = "uni_parents_missing" if variable == "duni_par3"


save "${data}\PISA2012_Averages.dta", replace 








			